package util
{
	import components.ActivityLabel;
	import components.ActivityLine;
	import components.FlowIcon;
	import components.MediatorButton;
	import components.ProcessCanvas;
	
	import editor.WorkflowexApp;
	
	import flash.geom.Point;
	
	import model.xpdl2.Activity;
	import model.xpdl2.IntermediateEvent;
	import model.xpdl2.Package;
	import model.xpdl2.Transition;
	import model.xpdl2.WorkflowProcess;
	
	import mx.collections.ArrayList;
	import mx.containers.Panel;
	import mx.core.Application;
	
	public class ProcessUtil
	{
		private var pack:Package;
				
		public function ProcessUtil(){
			
		}
		
		public static function saveXPDL(){}
		
		
		public static function getProcess(pack:Package):WorkflowProcess{
			return pack.getProcesses().getProcess();
		}
		
		
		
		
		
		public static function initCavas(pack:Package, flow:ProcessCanvas, app:WorkflowexApp, application:Application, drivePanel:Panel, monitorPanel:Panel, driveBtn:MediatorButton, monitorBtn:MediatorButton, config:ConfigUtil):void{
			flow.removeAllChildren();
			var activities:ArrayList=pack.getProcesses().getProcess().getActivities().getActivities();
			var i:int;
			for(i=0;i<activities.length;i++){
				var activity:Activity=Activity(activities.getItemAt(i));
				var icon:FlowIcon;
				var label:ActivityLabel;

				if(activity.getEvent()!=null){					
					
					if(activity.getEvent().getStartEvent()!=null){
						
						icon=new FlowIcon(Constants.STARTPATH,Constants.START);
						label=new ActivityLabel();
						
					}
					
					if(activity.getEvent().getEndEvent()!=null){
						icon=new FlowIcon(Constants.ENDPATH,Constants.END);
						label=new ActivityLabel();
					}
					
	 				if(activity.getEvent().getInterEvent()!=null){
						if(activity.getEvent().getInterEvent().trigger==IntermediateEvent.COMPENSATION){
							icon=new FlowIcon(Constants.COMPENPATH,Constants.COMPEN);
							label=new ActivityLabel();
						}
						if(activity.getEvent().getInterEvent().trigger==IntermediateEvent.TIMER){
							icon=new FlowIcon(Constants.TIMERPATH,Constants.TIMER);
							label=new ActivityLabel();
						}
						if(activity.getEvent().getInterEvent().trigger==IntermediateEvent.ERROR){
							icon=new FlowIcon(Constants.ERRORPATH,Constants.ERROR);
							label=new ActivityLabel();
						}
					}
					 
				}
				
				 if(activity.getImplementation()!=null){
					
					if(activity.getImplementation().getTask().getTaskScript()!=null){
						icon=new FlowIcon(Constants.SCRIPTPATH,Constants.SCRIPT);
						label=new ActivityLabel();
					}
					
					if(activity.getImplementation().getTask().getTaskService()!=null){
						icon=new FlowIcon(Constants.SERVICEPATH,Constants.SERVICE);
						label=new ActivityLabel();
					}
					
					if(activity.getImplementation().getTask().getTaskUser()!=null){
						icon=new FlowIcon(Constants.USERPATH,Constants.USER);
						label=new ActivityLabel();
					}
					
				}
				
				if(activity.getBlockActivity()!=null){
				
					icon=new FlowIcon(Constants.SUBPROCESSPATH,Constants.SUBPROCESS);
					label=new ActivityLabel();
					
				
				}
				
				if(activity.getRoute()!=null){
				
					if(activity.getRoute()().gateWayType==Constants.EXGATEWAYTYPE){
						icon=new FlowIcon(Constants.EXGATEWAYPATH,Constants.EXGATEWAY);
						label=new ActivityLabel();
					}
					
					if(activity.getRoute()().gateWayType==Constants.INGATEWAYTYPE){
						icon=new FlowIcon(Constants.INGATEWAYPATH,Constants.INGATEWAY);
						label=new ActivityLabel();
					}
					
					if(activity.getRoute()().gateWayType==Constants.PAGATEWAYTYPE){
						icon=new FlowIcon(Constants.PAGATEWAYPATH,Constants.PAGATEWAY);
						label=new ActivityLabel();
					}
				
				}
				 
				//icon.addEventListener(IconEvent.ICON_MOUSE_DOWN, onIconMouseDown);
				//icon.addEventListener(IconEvent.ICON_MOUSE_UP, onIconMouseUp);
				//icon.addEventListener(IconEvent.ICON_MOVE, onIconMove);
			    
			    label.setModel(activity);
			    
			    
				icon.label=activity.name;
				icon.width=32;
				icon.height=32;	
				
				label.styleName="iconButton";
							
				label.width=activity.getNodeGraphicsInfos().getNodeGraphicInfo().width;
				label.height=activity.getNodeGraphicsInfos().getNodeGraphicInfo().height;	
				label.x=activity.getNodeGraphicsInfos().getNodeGraphicInfo().getCoordinates().x;	
				label.y=activity.getNodeGraphicsInfos().getNodeGraphicInfo().getCoordinates().y;	
				
				icon.x=label.width/2-icon.width/2;
				icon.y=label.height/2-icon.height/2;
				
				label.addChild(icon);
				
				drivePanel=new Panel();
          		monitorPanel=new Panel();
          		drivePanel.x=0;
          		drivePanel.y=0;
          		drivePanel.width=0;
          		drivePanel.height=0;
          		monitorPanel.x=0;
          		monitorPanel.y=0;
          		monitorPanel.width=0;
          		monitorPanel.height=0;
          	
          		application.addChild(drivePanel);
          		application.addChild(monitorPanel);
				
				label.alpha=1.0;
				label.setApp(application);
				label.setDriveBtn(driveBtn);
				label.setDrivePanel(drivePanel);
				label.setMonitorBtn(monitorBtn);
				label.setMonitorPanel(monitorPanel);
				label.setConfig(config);
				
				flow.addChild(label);	
				
				flow.getLabels().addItem(label);
				
			}
			
			var transitions:ArrayList=pack.getProcesses().getProcess().getTransitions().getTransitions();
			
			for(i=0;i<transitions.length;i++){
				
				var transition:Transition=Transition(transitions.getItemAt(i));
				
				var fromId:String=transition.from;
				var toId:String=transition.tto;
				var trname:String=transition.name;
				
				var j:int;
				var count:int=0;
				
				var startPoint:Point=new Point();
				var endPoint:Point=new Point();
			
				var line:ActivityLine=new ActivityLine(0,0,0,0,0x000000,1,3,0x000000);
								
				for(j=0;j<flow.getLabels().length;j++){
					
					var activityLabel:ActivityLabel=ActivityLabel(flow.getLabels().getItemAt(j));
					if(activityLabel.getModel().id==fromId){
						startPoint.x=activityLabel.x+activityLabel.width;
						startPoint.y=activityLabel.y+activityLabel.height / 2;
						line.setFromActivity(ActivityLabel(flow.getLabels().getItemAt(j)));
						line.startX=startPoint.x;
						line.startY=startPoint.y;
						count++;
					}
					
					if(activityLabel.getModel().id==toId){
						endPoint.x=activityLabel.x;
						endPoint.y=activityLabel.y+activityLabel.height / 2;
						line.setToActivity(ActivityLabel(flow.getLabels().getItemAt(j)));
						line.endX=endPoint.x;
						line.endY=endPoint.y;
						count++;
					}
					
					if(count==2){
						break;
					}
					
				}
				
				
				line.name=trname;				
				line.getFromActivity().addOutputLine(line);
				line.getToActivity().addInputLine(line);
				flow.getLines().addItem(line);
				line.draw();
				flow.addChild(line);
				
				
				
			}
			
		}
		
		
		
		
		
		
		
		

	}
}